package com.adgox.tiantianbiting.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.adgox.tiantianbiting.TtbtApplication;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.main.MainActivity;
import com.adgox.tiantianbiting.mine.mobile.BindMobileActivity;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import java.util.HashMap;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean result = TtbtApplication.wxapi.handleIntent(getIntent(), this);
        if (!result) {
            finish();
        }
    }

    @Override
    public void onReq(BaseReq baseReq) {
        finish();
    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp != null) {
            if (baseResp.getType() == 1) {
                //登录
                if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
                    final String code = ((SendAuth.Resp) baseResp).code;
                    HashMap<String, String> map = new HashMap<>();
                    map.put("platSource", "android");
                    map.put("channel", "0");
                    Gson gson = new Gson();
                    String s = gson.toJson(map);
                    OkGo.<BaseResponseBean<UserInfo>>post(ApiConstant.BASE_URL + "app/wx/login/" + code)
                            .upJson(s)
                            .execute(new JsonCallback<BaseResponseBean<UserInfo>>() {
                                @Override
                                public void onSuccess(Response<BaseResponseBean<UserInfo>> response) {
                                    if (!"0".equals(response.body().getCode())) {
                                        Toast.makeText(getApplicationContext(), "获取微信信息失败", Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                    SessionManager.getDefault().setUser(response.body().getData());

                                    if (response.body().getData().getBandMobile() == 0) {
                                        //没绑手机号跳绑定页
                                        startActivity(new Intent(WXEntryActivity.this, BindMobileActivity.class));
                                        finish();
                                        return;
                                    } else {
                                        //绑定手机号，跳转首页个人中心
                                        Intent intent = new Intent(WXEntryActivity.this, MainActivity.class);
//                                        intent.putExtra("flag", "login");
                                        startActivity(intent);
                                        finish();
                                    }
                                }
                            });
                }
                //分享
            } else if (baseResp.getType() == 2){
                switch (baseResp.errCode){
                    case BaseResp.ErrCode.ERR_OK:
                        Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                    case BaseResp.ErrCode.ERR_USER_CANCEL:
                        Toast.makeText(this, "分享取消", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                    case BaseResp.ErrCode.ERR_AUTH_DENIED:
                        Toast.makeText(this, "分享被拒绝", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                }
            }
        }
    }
}
